package com.test;

import java.sql.*;

/**
 * 统计故障码以及每个故障码出现的总次数
 */

public class JavaHive02 {
    public static void main(String[] args) {

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            Class.forName("org.apache.hive.jdbc.HiveDriver");

            String url = "jdbc:hive2://192.168.200.200:10000/test";

            connection = DriverManager.getConnection(url,"root","root");

            preparedStatement = connection.prepareStatement("select error,error_code,count(error_code) from table_warning group by error,error_code");

            resultSet = preparedStatement.executeQuery();

            while(resultSet.next()){
                System.out.println("The error is :   "+resultSet.getString(1)+",  The error_code is "+resultSet.getString(2)+",  The error_code num is "+resultSet.getInt(3));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            if (resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (preparedStatement!=null){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }


    }
}
